package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.dto.CommentDto;
import com.qf.blog.dto.IndexPostDto;
import com.qf.blog.dto.MyPage;
import com.qf.blog.entity.Comment;
import com.qf.blog.entity.Post;
import com.qf.blog.entity.User;
import com.qf.blog.mapper.PostMapper;
import com.qf.blog.service.IPostService;
import com.qf.blog.utils.TimeAgoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 达哥
 * @since 2023-03-24
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Autowired
    private LikeService likeService;

    @Override
    public MyPage<IndexPostDto> indexPostData(IPage<IndexPostDto> page, Integer orderMode) {
        MyPage<IndexPostDto> indexPostDtoMyPage = baseMapper.indexPostDataPageVo(page, orderMode);
        List<IndexPostDto> records = indexPostDtoMyPage.getRecords();
        for (IndexPostDto record : records) {
            Post post = record.getPost();

            // 换成字符串的时间
            post.setCreateTimeStr(TimeAgoUtils.format(post.getCreateTime()));

            // 查询每个帖子的点赞数量
            Long count = likeService.likeSize(post.getId(), 1);
            post.setLikeCount(count);
        }
        return indexPostDtoMyPage;
    }

    @Override
    public IndexPostDto info(Integer id) {
        return baseMapper.info(id);
    }

    @Override
    public List<CommentDto> getCommentListByPostId(Integer id) {
        List<CommentDto> commentDtos = baseMapper.getCommentListByPostId(id);
        for (CommentDto commentDto : commentDtos) {
            Comment comment = commentDto.getComment();

            comment.setCreateTimeStr(TimeAgoUtils.format(comment.getCreateTime()));

            // 查询每个评论的点赞状态和状态数量
            Long count = likeService.likeSize(comment.getId(), 2);
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            User loginUser = (User) requestAttributes.getRequest().getSession().getAttribute("loginUser");
            boolean isLike = false;
            if (loginUser != null) {
                isLike = likeService.isLike(comment.getId(), 2, loginUser.getId());
            }
            commentDto.setLikeCount(count);
            commentDto.setLikeStatus(isLike ? 1L : 0L);
        }
        return commentDtos;
    }

    @Override
    public void updatePostSource(Integer postId, double source) {
        baseMapper.updatePostSource(postId,source);
    }

    @Override
    public List<IndexPostDto> adminPostList() {
        return baseMapper.adminPostList();
    }

    @Override
    public Integer updatePostType(Integer id, Integer type) {
        return baseMapper.updatePostType(id,type);
    }
}
